<?php

$curl_errno = 0;
require_once dirname(__FILE__) . '/init.php';
$timeStart = time();
$arrVpsData = require_once dirname(__FILE__) . '/input/vpslist.config.php';

$fileRunLockFile = dirname(__FILE__) . '/data/runtime/monitor.vps.lock';
fCheckDir($fileRunLockFile);
if (file_exists($fileRunLockFile)) {
    log::w('monitor is runing!');
    die();
}
file_put_contents($fileRunLockFile, date('Y-m-d H:i:s'));

$PbWarningVPS = array(
   // '172.245.5.169',
    //'172.245.5.150',
    //'192.210.195.104',
    '198.23.241.5',
    '103.27.126.125',
    '198.100.101.41'
);

$monitorIps = array(
);

foreach ($arrVpsData as $IpAddress => $vpsData) {
    if (in_array($IpAddress, $PbWarningVPS)) {
        continue;
    }
    $monitorIps[] = 'm' . str_replace('.', '-', $IpAddress) . '.u631.com';
}

$vpsCount = count($arrVpsData);
$warningCount = 0;


$HTMLBODY = '';
$HTMLBODY.= '<b>Stat By [' . getLocalIp() . '][' . $vpsCount . '个]服务器状态报告(' . date('Y-m-d H:i:s') . ')</b>
          <table border="1" width="100%" bordercolorlight="#C0C0C0" bordercolordark="#C0C0C0" style="border-collapse: collapse;font-size: 14px;">'.PHP_EOL;
$HTMLBODY.='<tr height="35">
    <td width="100" bgcolor="#CCCCFF"><b>IP&nbsp;</b></td>'.PHP_EOL.'
	<td width="200" bgcolor="#CCCCFF"><b>机房IDC</b></td>'.PHP_EOL.'
    <td width="100" bgcolor="#CCCCFF"><b>Mysqld&nbsp;</b></td>'.PHP_EOL.'
    <td width="100" bgcolor="#CCCCFF"><b>Httpd</b></td>'.PHP_EOL.'
    <td width="100" bgcolor="#CCCCFF"><b>网站数量</b></td>'.PHP_EOL.'
	<td width="100" bgcolor="#CCCCFF"><b>数据库连接</b></td>'.PHP_EOL.'
	<td width="100" bgcolor="#CCCCFF"><b>查询详情</b></td>'.PHP_EOL.'
	</tr>'.PHP_EOL.'
    ';

$isWarning = false;
$uptimeWarningCount = 0;

foreach ($monitorIps as $Ip) {
    $ISWCOUNTJIA = false;
    $url = 'http://' . $Ip . '/server_api.php?pass=' . md5('tom.1234') . '&time=' . date('Y-m-d H:i:s');
    $ip = str_replace('m', '', $ip);
    $ip = str_replace('-', '.', $ip);
	$ip = str_replace('.u631.com', '', $ip);
    $content = snoopy_fetch($url);
    !$content ? $content = snoopy_fetch($url) : true;
    if (!$content) {
        $isWarning = true;
        $HTMLBODY.='<tr height="35">
    <td colspan="7"><pre><font color="red">读取' . $Ip . '失败[' . $curl_errno . ']</font>&nbsp;&nbsp;|&nbsp;&nbsp;<a href="' . $url . '?isprint=1" target="_blank">查看API</a></pre>'.$ip.'[' . $arrVpsData[$ip]['idc'] . ']</td>
	</tr>
    ';
        $ISWCOUNTJIA = true;
        $warningCount++;
        continue;
    }
    $arrResult = json_decode($content, true);
    if (!$arrResult['ip']) {
        $arrResult['ip'] = $ip;
    }
    if (!is_array($arrResult) || empty($arrResult)) {
        $ISWCOUNTJIA = true;
        $warningCount++;
        $isWarning = true;
        $HTMLBODY.='<tr height="35">
    <td colspan="7"><pre><font color="red">读取' . $Ip . '成功[格式解析失败]</font>&nbsp;&nbsp;|&nbsp;&nbsp;<a href="' . $url . '?isprint=1" target="_blank">查看API</a>'.$ip.'[' . $arrVpsData[$ip]['idc'] . ']</pre></td>
	</tr>
    ';
        continue;
    }

    if ($arrResult['mysqld_count'] >= 3) {
        $mysqld = '<font color="green">OK</font>';
    } else {
        $mysqld = '<font color="red">WARNING</font>';

        if (!in_array($arrResult['ip'], $PbWarningVPS)) {
            $isWarning = true;
            $ISWCOUNTJIA = true;
        }
    }

    if ($arrResult['httpd_count'] >= 3) {
        $httpd = '<font color="green">OK</font>';
    } else {
        $httpd = '<font color="red">WARNING</font>';
        if (!in_array($arrResult['ip'], $PbWarningVPS)) {
            $isWarning = true;
            $ISWCOUNTJIA = true;
        }
    }

    $HTMLBODY.='<tr height="35">
    <td>' . $arrResult['ip'] . '</td>'.PHP_EOL.'
	<td>' . $arrVpsData[$arrResult['ip']]['idc'] . '</td>'.PHP_EOL.'
    <td>' . $arrResult['mysqld_count'] . '[' . $mysqld . ']</td>'.PHP_EOL.'
    <td>' . $arrResult['httpd_count'] . '[' . $httpd . ']</td>'.PHP_EOL.'
    <td>' . $arrResult['site_count'] . '</td>'.PHP_EOL.'
	 <td>' . $arrResult['mysqlconn'] . '</td>'.PHP_EOL.'
	<td><a href="' . $url . '?isprint=1" target="_blank">查看API</a></td>'.PHP_EOL.'
	</tr>
    ';
	$HTMLBODY1.=PHP_EOL;

    $uptime_red = false;
    $arr = explode('load average:', $arrResult['uptime']);
    $cputime = $arr[1];
    $arrcputime = explode(',', $cputime);
    foreach ($arrcputime as $strcputime) {
        $strcputime = trim($strcputime);
        if (intval($strcputime) > 4) {
            $uptime_red = true;
        }
    }

    if (!$uptime_red) {
        $HTMLBODY.='<tr height="35">
    <td colspan="7"><pre>uptime:' . $arrResult['uptime'] . '</pre></td>
	</tr>
    ';
	$HTMLBODY1.=PHP_EOL;
    } else {
        $uptimeWarningCount++;
        $HTMLBODY.='<tr height="35">
    <td colspan="7"><font color="red"><pre>uptime:' . $arrResult['uptime'] . '</pre></font></td>
	</tr>
    ';
	$HTMLBODY1.=PHP_EOL;
    }


    $HTMLBODY1.='<tr height="35">
    <td colspan="6" bgcolor="#B4CDCD"><pre>' . $arrResult['ip'] . '</pre></td>
	</tr>
    ';
	$HTMLBODY1.=PHP_EOL;

    $HTMLBODY1.='<tr height="35">
    <td colspan="6"><pre>uptime:' . $arrResult['uptime'] . '</pre></td>
	</tr>
    ';
	$HTMLBODY1.=PHP_EOL;

    $HTMLBODY1.='<tr height="35">
    <td colspan="6"><pre>df-h:' . $arrResult['df-h'] . '</pre></td>
	</tr>
    ';
	$HTMLBODY1.=PHP_EOL;

    $HTMLBODY1.='<tr height="35">
    <td colspan="6"><pre>logsize:' . $arrResult['logsize'] . '</pre></td>
	</tr>
    ';
	$HTMLBODY1.=PHP_EOL;

    $HTMLBODY1.='<tr height="35">
    <td colspan="6"><pre>mysqlsize:' . $arrResult['mysqlsize'] . '</pre></td>
	</tr>
    ';

	$HTMLBODY1.=PHP_EOL;

    $HTMLBODY1.='<tr height="35">
    <td colspan="6"><pre>free -m:' . $arrResult['free-m'] . '</pre></td>
	</tr>
    ';
	$HTMLBODY1.=PHP_EOL;

    if ($ISWCOUNTJIA) {
        $warningCount++;
    }
}

$HTMLBODY.=$HTMLBODY1 . '</table><hr />$PbWarningVPS:' . print_r($PbWarningVPS, true);

if ($isWarning) {
    $timeEnd = time();
    $HTMLBODY.='<hr />总计耗时:' . ($timeEnd - $timeStart) . '秒<hr />';
    post_email_by_sae('[严重警告(' . $warningCount . '个)]服务器(总计:' . $vpsCount . '个)异常(' . $warningCount . '个)高负载(' . $uptimeWarningCount . ')个[' . date('Y-m-d H:i:s') . ']', $HTMLBODY, $arrUsers);
    post_email_by_smtp('[严重警告(' . $warningCount . '个)]服务器(总计:' . $vpsCount . '个)异常(' . $warningCount . '个)高负载(' . $uptimeWarningCount . ')个[' . date('Y-m-d H:i:s') . ']', $HTMLBODY, $arrUsers);
} else {
    //发送正常邮件报告
    $time = time();
    $HTMLBODY.='</table>';
    $timeEnd = time();
    $HTMLBODY.='<hr />总计耗时:' . ($timeEnd - $timeStart) . '秒<hr />';
    post_email_by_sae('[服务器正常(' . $vpsCount . '个)]状态报告(总计:' . $vpsCount . '个)高负载(' . $uptimeWarningCount . ')个[' . date('Y-m-d H:i:s') . ']', $HTMLBODY, $arrUsers);
    post_email_by_smtp('[服务器正常(' . $vpsCount . '个)]状态报告(总计:' . $vpsCount . '个)高负载(' . $uptimeWarningCount . ')个[' . date('Y-m-d H:i:s') . ']', $HTMLBODY, $arrUsers);
}

unlink($fileRunLockFile);
?>